﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using OnlineManagement.Model;
using OnlineManagement.IDAL;
using Velox.Utility;
namespace OnlineManagement.Web.WebBLL
{
    public class UserManagement
    {
        /// <summary>
        /// the user name is exsited in database. if exsited return true else return false.
        /// the user name has been existed when return true.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool IsExsitedUserName(string userName)
        {
            OnlineManagement.BLL.TempRegist tr = new OnlineManagement.BLL.TempRegist();
            if (string.IsNullOrEmpty(userName))
            {
                return false;
            }
            MembershipUserCollection user = Membership.FindUsersByName(userName);
            if (user != null && user.Count > 0)
            {
                return true;
            }
            return false;
        }

        public RegisterMsg TempRegestUser(TempRegistUsers user,out int hoggeryId)
        {
            RegisterMsg msg = null;
            hoggeryId = 0;
            MembershipUser onlineUser = null;
            try
            {
                if (user != null)
                {
                    onlineUser = Membership.CreateUser(user.LoginName, user.Password);
                    if(onlineUser==null){
                        msg = new RegisterMsg() {Flag=false,MessageId=RegisterMsg.ErrorMessage.Unknown.ToString(),Message="注册失败，请重试" };
                        return msg;
                    }
                    Roles.AddUserToRole(user.LoginName, OnlineManagement.Common.PubConstant.OnlineUser);
                    OnlineManagement.BLL.TempRegist tr = new OnlineManagement.BLL.TempRegist();
                    string returnValue = string.Empty;
                    bool flag = tr.CreateHoggery(user, out returnValue,out hoggeryId);
                    if (flag)
                    {
                        msg = new RegisterMsg() { Flag = true, Message = returnValue, MessageId = "Ok" };
                    }
                    else
                    {
                        msg = new RegisterMsg() { Flag = false, MessageId = RegisterMsg.ErrorMessage.Unknown.ToString(), Message = "服务器发生错误，申请注册失败，请稍后重试" };
                    }

                }
                else
                {
                    msg = new RegisterMsg() { Flag = false, MessageId = RegisterMsg.ErrorMessage.NullReferences.ToString(), Message = "用户信息为空，不能完成注册。" };
                }
                return msg;
            }
            catch (Exception ex)
            {
                msg = new RegisterMsg() { Flag = false, Message = "服务器发生错误，申请注册失败，请稍后重试" };
                if(onlineUser!=null){
                    Membership.DeleteUser(user.LoginName, true);
                }
                return msg;
            }
        }

    }
}